package socket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class LoginServer {
	public static void main(String[] args) {
		try {
			// 创建服务器端Socket
			ServerSocket ss = new ServerSocket(4000);
			// 使用accept()等待客户的通信
			Socket s = ss.accept();
			// 获得输入流，得到用户的请求
			InputStream is = s.getInputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String info;
			while((info = br.readLine()) != null){
				System.out.println("客户机说：" + info);
			}
			// 给客户端一个响应
			OutputStream os = s.getOutputStream();
			String reply = "欢迎登陆";
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(reply);
			bw.flush();
			// 释放资源
			bw.close();
			os.close();
			br.close();
			is.close();
			s.close();
			ss.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
